<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing: Component
 *
 * @category   	Gnn Cms
 * @package    	Gnn Component
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 Jun 9, 2013 || 11:19:25 PM
 */
class Zendvn_Component_Component {
	public $id;
	public $name;
	public $client_id;
	public $published;
	public $access;
	public $protected;
	public $params;
	public $checked_out;
	public $checked_out_time;
	public $ordering;
	
	public $gcComponentPath;
	public $gcComponentUrl;
	public $gcConfig;
	public $gcPace;
	public function __construct($gcPage) {
		$this->gcPage = $gcPage;
		if($this->gcPage->id >0){
			$this->_loadComponent ();
		}else{
			$this->_loadComponentAjax();
		}
		$this->_getComponentConfig();
	}
	
	
	/**
	 * load component from database
	 * 
	 * @param integer $id        	
	 */
	private function _loadComponent() {
		$db = Zend_Registry::get ( REGISTRY_KEY_DBO );
		$sql = $db->select ()
				  ->from ( TBL_PRF . "component" , array('id','name','client_id','access','params'))
				  ->where ( "id = ?", $this->gcPace->copeid );
		$component = $db->fetchRow ( $sql );
		
		$this->id 			= $component ['id'];
		$this->access 		= $component ['access'];
		$this->client_id 	= $component ['client_id'];
		$this->name 		= $component ['name'];
		$this->params 		= ($component['params'] =="") ? null : json_decode($component['params']);
		
		$this->gcComponentPath      	= ($this->client_id == 1) ? COMPONENTS_PATH.DS.'frontend'.DS.$this->name
															  	  : COMPONENTS_PATH.DS.'backend' .DS.$this->name;
		$this->gcComponentUrl			= ($this->client_id == 1) ? COMPONENTS_URL .DS.'frontend'.DS.$this->name
															  	  : COMPONENTS_URL .DS.'backend' .DS.$this->name;
	}
	
	/**
	 * load component if component id <0 : ajax situation
	 */
	private function _loadComponentAjax($component_name) {
	
		$this->id 			= $component ['id'];
		$this->access 		= $component ['access'];
		$this->client_id 	= $component ['client_id'];
		$this->name 		= $component ['name'];
		$this->params 		= ($component['params'] =="") ? null : json_decode($component['params']);
	
		$this->gcComponentPath      	= ($this->client_id == 1) ? COMPONENTS_PATH.DS.'frontend'.DS.$this->name
		: COMPONENTS_PATH.DS.'backend' .DS.$this->name;
		$this->gcComponentUrl			= ($this->client_id == 1) ? COMPONENTS_URL .DS.'frontend'.DS.$this->name
		: COMPONENTS_URL .DS.'backend' .DS.$this->name;
	}
	
	
	/**
	 * Get view path
	 * @param String $ctrl : controller
	 * @param String $actv : activity
	 * @return string
	 */
	public function getView($ctrl,$actv){
		return $this->gcComponentPath.DS.'views'.$ctrl.DS.'tmpl'.DS.$actv.'.php';
	}
	
	/**
	 * Get component config from file
	 * @param string $configurationFile
	 * @return Zend_Config_Ini
	 */
	private function _getComponentConfig($configurationFile = 'component.ini'){
		$configurationFilePath = $this->gcComponentPath.DS.$configurationFile;
		$gcConfig 	   		   = new Zend_Config_Ini($configurationFilePath,'component');
		$this->gcConfig        = (object)$gcConfig->toArray();
	}
}